package com.allwees.bs.c.module.base.dto;

import com.allwees.bs.core.modelbase.constant.ResultEnum;
import com.allwees.bs.core.modelbase.exception.BusinessException;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;


public abstract class BaseDto<T> implements Serializable {
	@SuppressWarnings("unchecked")
	public T buildToField(String... fields) {
		Class<? extends BaseDto> clazz = this.getClass();
		Object obj;
		try {
			obj = clazz.getConstructor().newInstance();
			for(String field : fields) {
				Field f = clazz.getDeclaredField(field);
				f.setAccessible(true);
				f.set(obj, f.get(this));
			}
		} catch(InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
				| NoSuchFieldException e) {
			throw BusinessException.getInstance(ResultEnum.CONVERT_ERROR);
		}
		return (T) obj;
	}

	// abstract T buildDtoInstance(T2 entity);

}
